package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    RoleDao roleDao;

    @Autowired
    PermissionDao permissionDao;

    @Autowired
    MenuDao menuDao;


    /**
     * 1. 在mybatis中实现一对多的查询
     * 2. 根据用户名查询用户对象
     * 根据用户id查询角色对象
     * 根据每个角色的id查询相应的权限对象
     * 组合数据
     *
     * @param username
     * @return
     */
    @Override
    public SysUser findByUsername(String username) {
        //根据用户名查询用户对象
        SysUser sysUser = userDao.findByUsername(username);
        //根据用户id查询角色对象
        Set<Role> roleSet = roleDao.findRolesByUserId(sysUser.getId());
        //根据每个角色的id查询相应的权限对象
        for (Role role : roleSet) {
            Set<Permission> permissionSet = permissionDao.findPermissionsByRoleId(role.getId());
            //把权限列表添加到角色中
            role.setPermissions(permissionSet);
        }
        //把角色列表添加到用户对象中
        sysUser.setRoles(roleSet);
        return sysUser;
    }

    //根据用户名查询 角色 根据角色查菜单
    @Override
    public List<Map<String, Object>> findMenuByUsername(String username) {
        //根据用户名查询所有对应的菜单项 (没有子菜单)
        List<Map<String, Object>> mapList = userDao.findMenuByUsername(username);
        for (Map<String, Object> map : mapList) {
            //遍历 得到每一项主菜单  根据id查子菜单
            int id = Integer.parseInt(String.valueOf(map.get("id")));
            //把查出来的子菜单
            List<Map<String,Object>> childrenListMap=menuDao.findChildrenById(id);
            for (Map<String, Object> stringObjectMap : childrenListMap) {
                //遍历拼接 查询到地址 因为与页面要跳转的不一致
                String linkUrlDB = String.valueOf(stringObjectMap.get("linkUrl"));
                String linkUrl = "/pages/"+linkUrlDB;
                stringObjectMap.put("linkUrl",linkUrl);
            }
            // 拼接到主菜单下
            map.put("children",childrenListMap);
        }
        return mapList;
    }

    @Override
    public void add(SysUser user, Integer[] rolesIds) {
        userDao.add(user);

        if (user.getId() != null && rolesIds != null && rolesIds.length > 0) {
            for (Integer rolesId : rolesIds) {
                userDao.set(user.getId(), rolesId);

            }

        }
    }


    @Override
    public long findByname(String username) {
        return userDao.findByname(username);
    }

    @Override
    public void update(String username, String password) {
        String encode = new BCryptPasswordEncoder().encode(password);
        userDao.update(username,encode);
    }
    @Override
    public void edit(SysUser user, Integer[] rolesIds) {
        userDao.edit(user);
        //删除关系
        userDao.delRelation(user.getId());
        if (user.getId() != null && rolesIds != null && rolesIds.length > 0) {
            for (Integer rolesId : rolesIds) {
                userDao.set(user.getId(), rolesId);
            }

        }
    }

    @Override
    public List<SysUser> findAll() {

        return userDao.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<SysUser> page = userDao.findByCondition(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page);
        return pageResult;
    }

    @Override
    public void delById(Integer id) {
        //根据用户id查询关联角色个数
        userDao.deleteRelation(id);
        userDao.delById(id);


    }

    @Override
    public SysUser findById(Integer id) {
        return userDao.findById(id);
    }


    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }

    @Override
    public List<Role> findAllRoles() {
        return userDao.findAllRoles();
    }
}
